<?php
/**
 * Created by PhpStorm.
 * User: 陶然
 * Date: 2019/4/20
 * Time: 9:40
 * SOURCE: 中国大陆工时规管现况（标准工时）：
 *              一天工作时间为8小时，平均每周工时不超过40小时;
 *              加班上限为一天3小时及一个月36小时，逾时工作薪金不低于平日工资的150％。
 *              而一周最高工时则为48小时。平均每月计薪天数为21.75天。
 * TO: 我是陶然,健康撸代码,拒绝通宵。
 * 版权所有: © 2019-2030 陶然
 * WARNING: 未经本人许可, 禁止一切商用
 * Mail: xiaka53@vip.qq.com
 * CLASS: < 我的活动 >
 */
namespace App\HttpController\Taoran;

use App\HttpController\V1\Auth;

class Myagglon extends Auth
{
    /**
     * @return bool|void
     * 我的活动
     */
    function index()
    {
        $page=$this->input('page','post');
        $data=$this->db->where('user_id',$this->user->id)->orderBy('id','desc')->where('status',1)->get('activitys',[($page-1)*10,10],'*');
        $list=[];
        foreach ($data as $k=>$v){
            $val=$this->db->where('id',$v['activity_id'])->getOne('study','id,study_title,image,city,date,time,market_price');
            if (!strstr($val['image'],'http://')){
                $val['image']=QINIU_URL.$val['image'];
            }
            $val['city']=str_replace('/',' ',$val['city']);
            $val['date']=date('m/d',strtotime($val['date']));
            $turn_time=date('h:i',strtotime($val['date']));
            $on_time=date('h:i',strtotime($val['date'])+$val['time']*60*60);
            if ($on_time<$turn_time){
                $on_time=$on_time.'(次日)';
            }
            $data[$k]['time']=$turn_time.'-'.$on_time;
            $val['market_price']=$v['people'].'个名额';
            if (strtotime($val['date'])+$val['time']*60*60<time()){
                $val['market_price']='已结束';
            }
            $val['id']=$v['id'];
            $list[]=$val;
        }
        return $this->writeJson(200,$list,'获取成功');
    }

    /**
     * @return bool
     * 详情
     */
    function details()
    {
        $ids=$this->input('ids','post');
        if (intval($ids)!=$ids){
            return $this->writeJson(201,'','传值错误');
        }
        $activitys=$this->db->where('user_id',$this->user->id)->where('id',$ids)->where('status',1)->getOne('activitys','*');
        if (!$activitys){
            return $this->writeJson(212,'','尚未参加此活动');
        }
        $data=$this->db->where('id',$activitys['activity_id'])->getOne('study');
        if (!$data){
            return $this->writeJson(202,'','不存在该活动');
        }
        $turn_time=date('h:i',strtotime($data['date']));
        $on_time=date('h:i',strtotime($data['date'])+$data['time']*60*60);
        if ($on_time<$turn_time){
            $on_time=$on_time.'(次日)';
        }
        $status=1;
        $collection_status=1;
        $adminHome=[];
        $this->db->where('id',$ids)->setInc('study','browse',1);

        if ($this->db->where('class_id',$ids)->where('user_id',$this->user->id)->getOne('collection')){
            $collection_status=2;
        }
        if ($this->db->where('user_id',$this->user->id)->where('activity_id',$ids)->getOne('activitys')){
            $status=2;
        }
        if (strtotime($data['date'])+$data['time']*60*60<time()){
            $status=3;
        }
        $data['introduce']=str_replace('src="','src="'.ADMIN_URL,$data['introduce']);
        $list=[
            'id'=>$data['id'],
            'user_id'=>$this->user->id,
            'image'=>!strstr($data['image'],'http://')?QINIU_URL.$data['image']:$data['image'],
            'name'=>$data['study_title'],
            'browse'=>$data['browse'],
            'favorite'=>$data['favorite'],
            'date'=>date('m/d',strtotime($data['date'])),
            'time'=>$turn_time.'-'.$on_time,
            'address'=>str_replace('/',' ',$data['city']).'  '.$data['address'],
            'vip_price'=>$data['vip_price'],
            'market_price'=>$data['market_price'],
            'content'=>$data['introduce'],
            'status'=>$status,  //1正常 2报名  3结束
            'collection_status'=>$collection_status,  //1未收藏 2收藏
        ];
        $data=['list'=>$list,'user'=>$adminHome];
        return $this->writeJson(200,$data);
    }
}